﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System.Linq;
using Subsonic.ViewModels;
using Subsonic.Data;
using MichMan.Utilities.Requests;
using System.Windows;

namespace Subsonic.ViewModels
{
    public class MusicLibraryItemViewModel : BasePivotItemViewModel<Group<ArtistItem>>
    {
        public MusicLibraryItemViewModel()
        {
            Title = "{music_library_label}";
            Id = Title;
        }

        public override IRequest LoadItems()
        {

            GetIndexesRequest req = new GetIndexesRequest();
            req.RunRequestCompleted += new MichMan.Utilities.Requests.RunRequestCompletedEventHandler(getIndexes_RunRequestCompleted);
            req.RunRequestAsync();
            return req;
        }

        void getIndexes_RunRequestCompleted(object sender, MichMan.Utilities.Requests.RunRequestCompletedEventArgs e)
        {
            GetIndexesRequest getIndexes = (GetIndexesRequest)sender;
            if (getIndexes.Response == null || getIndexes.RequestStatus != RequestStatus.Succeeded)
            {
                // TODO:  Display error
                return;
            }

            foreach (var idx in getIndexes.Response.Indexes.Indecies)
            {
                Group<ArtistItem> aig = new Group<ArtistItem>(idx.Name, from a in idx.Artist select new ArtistItem(a));
                Items.Add(aig);
            }
        }
    }
}